package com.sg.plugin.calc.aop;

import com.wicket.okrcalc.common.dto.MetaDataDto;
import com.wicket.okrcalc.common.util.MetaDataHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;


/**
 * @author ：CHS
 * @version 1.0
 * @since 2022/6/10 13:43
 */

@Component
@Aspect
@Slf4j
public class MethodAspect {
    @Pointcut("execution(* com.wicket.okrcalc.base.service.impl.*.*(..))")
    public void methodPointcut() {
    }

//    @Around("methodPointcut()")
//    public Object checkParam(ProceedingJoinPoint joinPoint) {
//        //        try {

    /// /            Object[] args = joinPoint.getArgs();
    /// /            if (args == null || args.length == 0) {
    /// /                return joinPoint.proceed();
    /// /            }
    /// /            MetaDataHelper.putMetaData(args[0]);
    /// /            MetaDataDto metaData = MetaDataHelper.getMetaData(args[0]);
    /// /            if (metaData != null) {
    /// /                if (metaData.getRequiredFieldList().size() == 0) {
    /// /                    //如果没有必填字段,任何场景都是正常执行
    /// /                    return joinPoint.proceed();
    /// /                }
    /// /                if (metaData.isAllRequired()) {
    /// /                    if (!checkRequiredFieldList(args[0])) {
    /// /                        //如果全都是必填字段，并且发现必填字段有空值,执行跳过
    /// /                        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    /// /                        Class claszz = signature.getReturnType();
    /// /                        return claszz.newInstance();
    /// /                    } else {
    /// /                        //如果全都是必填字段，并且发现必填字段无空值，正常执行
    /// /                        return joinPoint.proceed();
    /// /                    }
    /// /                } else {
    /// /                    if (!checkRequiredFieldList(args[0])) {
    /// /                        //如果一部分是必填字段，一部分是非必填字段，并且发现必填字段有空值，抛异常
    /// /                        throw new BizException("-1", "必填参数校验不通过", false);
    /// /                    } else {
    /// /                        //如果一部分是必填字段，一部分是非必填字段，并且发现必填字段无空值，正常执行
    /// /                        return joinPoint.proceed();
    /// /                    }
    /// /                }
    /// /            }
    /// /
    /// /        } catch (Exception e) {
    /// /            e.printStackTrace();
    /// /        } catch (Throwable throwable) {
    /// /            throwable.printStackTrace();
    /// /        }
//    }
    private boolean checkRequiredFieldList(Object param) {
        boolean bRet = true;
        try {
            Field[] fields = param.getClass().getDeclaredFields();
            MetaDataDto metaData = MetaDataHelper.getMetaData(param);
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(param);
                if (value == null && metaData.getRequiredFieldList().contains(field)) {
                    bRet = false;
                    break;
                }
            }
        } catch (Exception e) {

        }
        return bRet;
    }
}
